﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace DELVE_GAME.ViewStructure
{
    class GameboardComponent : ViewComponent
    {
        GameboardCell[][] cells;
        //private System.Data.DataTable dataTable;
        private int cols;
        private int rows;
        private Sprite potencyValuesSprite;
        private int cellWidth;
        private int cellHeight;
        private Game game;

        public GameboardComponent(List<List<GameboardCell>> cells, Game game, int x, int y, int width, int height, int cols, int rows, Sprite potencyValueImages)
        {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.cols = cols;
            this.rows = rows;
            this.potencyValuesSprite = potencyValueImages;
            this.cellWidth = width / cols;
            this.cellHeight = height / rows;
            this.game = game;

            this.cells = new GameboardCell[cells.Count][];
            for (int i = 0; i < cells.Count; i++)
            {
                this.cells[i] = new GameboardCell[cells[i].Count];
                cells[i].CopyTo(this.cells[i]);
            }

            foreach (GameboardCell[] ca in this.cells)
            {
                foreach (GameboardCell c in ca)
                {
                    c.setOffset(this.x, this.y);
                    c.setWidth(cellWidth);
                    c.setHeight(cellHeight);
                }

            }
        }

        public GameboardComponent(List<List<GameboardCell>> cells, int x, int y, int width, int height, int cols, int rows)
        {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.cols = cols;
            this.rows = rows;
            //this.potencyValuesSprite = potencyValueImages;
            this.cellWidth = width / cols;
            this.cellHeight = height / rows;
            /*List<GameboardCell>[] tempList = cells.ToArray();
            this.cells = new GameboardCell[cells.Count][];
            for(int i=0; i<tempList.Count(); i++)//List<GameboardCell> l in tempList)
            {
                this.cells[i] = tempList[i].ToArray();
            }
            foreach (GameboardCell[] ca in this.cells)
            {
                foreach (GameboardCell c in ca)
                {
                    c.setOffset(this.x, this.y);
                    c.setWidth(cellWidth);
                    c.setHeight(cellHeight);
                }

            }*/
            this.cells = new GameboardCell[cells.Count][];
            for (int i = 0; i < cells.Count; i++)
            {
                this.cells[i] = new GameboardCell[cells[i].Count];
                cells[i].CopyTo(this.cells[i]);
            }

            foreach (GameboardCell[] ca in this.cells)
            {
                foreach (GameboardCell c in ca)
                {
                    c.setOffset(this.x, this.y);
                    c.setWidth(cellWidth);
                    c.setHeight(cellHeight);
                }

            }
            //this.game = game;
        }

        public int getX()
        {
            return this.x;
        }

        public int getY()
        {
            return this.y;
        }
        public Game getGame() {
            return this.game;
        }
        public override void draw(Microsoft.Xna.Framework.Graphics.SpriteBatch sb)
        {

            for (int i = 0; i < cells.Count(); i++)
            {
                for (int j = 0; j < cells[i].Count(); j++)
                {
                    cells[i][j].draw(sb);
                }
            }

            if (potencyValuesSprite != null)
            {
                for (int j = 0; j < 3; j++)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        if (PotencyPeeker.grid[i][j] != 0)
                        {
                            potencyValuesSprite.draw(sb, PotencyPeeker.x + i * cellWidth, PotencyPeeker.y + j * cellHeight, PotencyPeeker.grid[i][j] - 1, 1.0, 1.0, Color.White);
                        }
                    }
                }
            }
        }

        public override bool checkCollision(int x, int y)
        {
            int left = this.x;
            int right = this.x + cols * cellWidth;
            int top = this.y;
            int bottom = this.y + rows * cellHeight;


            return (x>=left && y>= top && x<right && y<bottom);
        }

        public override void mouseOver(int x, int y)
        {
            //foreach (GameboardCell[] ca in cells)
            //{
                //foreach (GameboardCell c in ca)
                //{
            if (!checkCollision(x, y))
            {
                View.setHudText("");
                PotencyPeeker.grid[0][0] = 0;
                PotencyPeeker.grid[0][1] = 0;
                PotencyPeeker.grid[0][2] = 0;
                PotencyPeeker.grid[1][0] = 0;
                PotencyPeeker.grid[1][1] = 0;
                PotencyPeeker.grid[1][2] = 0;
                PotencyPeeker.grid[2][0] = 0;
                PotencyPeeker.grid[2][1] = 0;
                PotencyPeeker.grid[2][2] = 0;
            }
            else
            {
                for (int i = 0; i < cells.Count(); i++)
                {
                    for (int j = 0; j < cells[i].Count(); j++)
                    {
                        GameboardCell c = cells[i][j];
                        if (c.checkCollision(x, y))
                        {
                            PotencyPeeker.x = c.getX() - cellWidth;
                            PotencyPeeker.y = c.getY() - cellHeight;
                            PotencyPeeker.grid[0][0] = getCellPotency(i - 1, j - 1);
                            PotencyPeeker.grid[0][1] = getCellPotency(i, j - 1);
                            PotencyPeeker.grid[0][2] = getCellPotency(i + 1, j - 1);
                            PotencyPeeker.grid[1][0] = getCellPotency(i - 1, j);
                            PotencyPeeker.grid[1][1] = getCellPotency(i, j);
                            PotencyPeeker.grid[1][2] = getCellPotency(i + 1, j);
                            PotencyPeeker.grid[2][0] = getCellPotency(i - 1, j + 1);
                            PotencyPeeker.grid[2][1] = getCellPotency(i, j + 1);
                            PotencyPeeker.grid[2][2] = getCellPotency(i + 1, j + 1);
                        }
                        c.mouseOver(x, y);
                    }
                }
            }
        }

        private int getCellPotency(int x, int y)
        {
            int value = 0;

            /*Jonathan change*/if ((x >= 0 && y >= 0) && (x < rows && y < cols))
            {
                Virus v = cells[x][y].getTile().getVirus();
                if (v != null && game.isVisible(cells[x][y].getTile()))
                {
                    value = v.getViewPotency();
                }
            }
            return value;
        }

        public GameboardCell[][] getCells()
        {
            return cells;
        }



        public override void mousePressed(int x, int y)
        {
            foreach (GameboardCell[] ca in cells)
            {
                foreach (GameboardCell c in ca)
                {
                    c.mousePressed(x, y);
                }
            }
        }

        public override void mouseReleased(int x, int y)
        {
            foreach (GameboardCell[] ca in cells)
            {
                foreach (GameboardCell c in ca)
                {
                    c.mouseReleased(x, y);
                }
            }
        }

        private class PotencyPeeker
        {
            public static int x = 0;
            public static int y = 0;
            public static int[][] grid = new int[][] { new int[] { 0, 0, 0 }, new int[] { 0, 0, 0 }, new int[] { 0, 0, 0 } };
        }


        public override void update()
        {
            base.update();
        }

        public int getCellWidth()
        {
            return this.cellWidth;
        }

        public int getCellHeight()
        {
            return this.cellHeight;
        }

        public int getNumRows()
        {
            return this.rows;
        }
        public int getNumCols()
        {
            return this.cols;
        }
    }
}